/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ncsu.gps;

import com.sun.spot.core.peripheral.Spot;

/**
 *
 * @author me
 */
public class GPSLockRecorder {
    private static final String LONGITUDE   = "LONGITUDE";
    private static final String LATITUDE    = "LATITUDE";
    private static final String ALTITUDE    = "ALTITUDE";

    /**
     * records latitude, longitude and altitide to persistent properties
     * @param latitude
     * @param longitude
     * @param altitide
     */
    public void recordCoords(double lastLat, double lastLon, double lastAlt) {
        if (lastLat != Double.MAX_VALUE && lastLon != Double.MAX_VALUE && lastAlt != Double.MAX_VALUE) {
            Spot.getInstance().setPersistentProperty(LATITUDE, ""+lastLat);
            Spot.getInstance().setPersistentProperty(LONGITUDE, ""+lastLon);
            Spot.getInstance().setPersistentProperty(ALTITUDE, ""+lastAlt);
        }
    }

    /**
     * reads latitude, longitude and altitude from persistent properties, if they have previously been recorded
     * @return 3-double array with latitude, longitude and altitude in that order
     */
    public double[] readCoords() {
        System.out.println("Initializing coordinates...");
        double[] theAnswer = new double[3];
        if (Spot.getInstance().getPersistentProperty(LATITUDE) != null &&Spot.getInstance().getPersistentProperty(LONGITUDE) != null &&Spot.getInstance().getPersistentProperty(ALTITUDE) != null) {
            theAnswer[0] = Double.parseDouble(Spot.getInstance().getPersistentProperty(LATITUDE));
            theAnswer[1] = Double.parseDouble(Spot.getInstance().getPersistentProperty(LONGITUDE));
            theAnswer[2] = Double.parseDouble(Spot.getInstance().getPersistentProperty(ALTITUDE));
            return theAnswer;
        }
        return null;
    }

}
